package pta05;

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;

public class InvertedIndex_pta554 {

	public static void main(String[] args) {
		TreeMap<String,TreeSet<Integer>> mapword=new TreeMap<String,TreeSet<Integer>>();
		Map<Integer,String>map=new TreeMap<Integer,String>();
		Scanner in=new Scanner(System.in);
		int linenum=1;
		while(in.hasNext()) {
			String str=in.nextLine();
	        if(str.equals("!!!!!")) {
	        	break;
	        }
	        map.put(linenum, str);
			Scanner str1=new Scanner(str);
			while(str1.hasNext()){
				String word=str1.next();
				if(!mapword.containsKey(word)){
					TreeSet<Integer> set=new TreeSet<Integer>();
					set.add(linenum);
					mapword.put(word, set);
				}
				else{
					mapword.get(word).add(linenum);
				}
			}
			linenum++;
			str1.close();
			in.close();
		}
		for(Map.Entry<String, TreeSet<Integer>> entry:mapword.entrySet()){
			System.out.println(entry);
		}
		while(in.hasNext()){
			String str=in.nextLine();
			if(str.length()==0){
				System.out.println("found 0 results");
			}
			String[] strarr=str.split(" ");
			TreeSet<Integer> set=new TreeSet<Integer>();
			for (int i = 0; i < linenum-1; i++) {
				for (int j = 0; j < strarr.length; j++) {
					if (!mapword.containsKey(strarr[j])||set.retainAll(mapword.get(strarr[j]))) {
						break;
					} else {
						set=mapword.get(strarr[j]);
					}
				}
			}
			if(set.isEmpty()){
				System.out.println("found 0 results");
			}
			else{
				System.out.println(set);
				for(Integer e:set){
					System.out.println("line "+e+":"+map.get(e));
				}
			}
			in.close();
		}
	}
}
/*TreeSet<Integer> set=new TreeSet<Integer>();
Scanner str1=new Scanner(str);
for (int i = 0; i < linenum; i++) {
	while(str1.hasNext()){
		String word=str1.next();
		if (!mapword.containsKey(word)||set.retainAll(mapword.get(word))) {
			break;
		} else {
			set=mapword.get(word);
		}
	}
}
if(set.isEmpty()){
	System.out.println("found 0 results");
}
else{
	System.out.println(set);
	for(Integer e:set){
		System.out.println("line "+e+":"+map.get(e));
	}
}*/
